var checkNetWork = require("../../utils/CheckNetWork.js")
var md5 = require("../../utils/MD5.js")
var config = require("../../utils/config.js")
Page({
  data:{
    getCodeBtnProperty: {
      titileColor: '#B4B4B4',
      disabled: false,
      loading: false,
      title: '获取验证码'
    },
    loginBtnProperty: {
      disabled: true,
      loading: false,
    },
    registerParams: {
      phone: '',
      code: '', 
      checksum: ''
    },
    codeTfFocus: false,
  },
  onLoad:function(options){
    // 生命周期函数--监听页面加载
    
  },

  //输入手机号
  phoneInput: function(e) {
    var that = this
    var inputValue = e.detail.value
    var length = e.detail.value.length
    if (length == 11) {
      //给接口的phone参数赋值,以及改变获取验证码的状态
      that.setData({
        'registerParams.phone': inputValue,
        'getCodeBtnProperty.titileColor':'#34B5E3',
        'getCodeBtnProperty.disabled': false
      })
    }else {
      //给接口的phone参数赋值,以及改变获取验证码的状态
      that.setData({
        'registerParams.phone': '',
        'getCodeBtnProperty.titileColor':'#B4B4B4',
        'getCodeBtnProperty.disabled': true
      })
    }
  },

  //获取验证码
  onGetVerify: function() {
    //请求接口
    if (checkNetWork.checkNetWorkStatu() == false) {
      console.log('网络错误')
      wx.showToast({
        title: '网络错误',
        duration: 2
      })
    }else {
      var that = this
      that.setData({
        'getCodeBtnProperty.loading': true
      })
      wx.request({
        url: config.url_root +'/users/getSmsPin',
        data: {
          'phone': that.data.registerParams.phone
        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        success: function (res) {
          var message = res.data.message
          var msg = res.data.msg
          if (msg === 'OK') {
            wx.showToast({
              title: '已发送',
              icon: 'success',
              duration: 2000,
            })
            //启动定时器
            var number=60;
            var time = setInterval(function(){
              number--
             that.setData({
                'getCodeBtnProperty.title':number + '秒',
                'getCodeBtnProperty.disabled': true
              })
             if (number == 0) {
                that.setData({
                  'getCodeBtnProperty.title':'重新获取',
                  'getCodeBtnProperty.disabled': false
                })
                clearInterval(time)
              }
            }, 1000)
          } else {
            wx.showToast({
              title: '发送失败',
              duration: 2000
            })
          }
          //光标下移
          that.setData({
            'codeTfFocus': true
          })
        },
        fail: function(res) {
          // fail
          console.log(res)
          that.failMessage()
        },
        complete: function() {
          // complete
          //隐藏loading
          that.setData({
            'getCodeBtnProperty.loading': false
          })
        }
      })
    }
  },

  //输入验证码
  codeInput: function(e) {
    var that = this
    var inputValue = e.detail.value
    var length = e.detail.value.length
    if (length == 6) {
      //给接口的phone参数赋值,以及改变获取验证码的状态
      console.log("code:xxx: ", inputValue)
      that.setData({
        'loginBtnProperty.disabled': false,
        'registerParams.code': inputValue
      })
    }else {
      //给接口的phone参数赋值,以及改变获取验证码的状态
      that.setData({
        'loginBtnProperty.disabled': true,
        'registerParams.code': ''
      })
    }
  },

  // 绑定手机号码
  onReg: function() {
    //光标取消
    var that = this
    that.setData({
      'codeTfFocus': true
    })
    //请求接口
    if (checkNetWork.checkNetWorkStatu() == false) {
      console.log('网络错误')
    } else {
      that.setData({
        // 显示loading
        'loginBtnProperty.loading': true
      })

      var cs = that.data.registerParams.code + that.data.registerParams.phone
      // cs = md5.hexMD5(cs)
      console.log("code: ", that.data.registerParams.code)

      var openid = wx.getStorageSync('openid')

      wx.request({
        url: config.url_root + '/addUserInfo',
        data:{
          'phone': that.data.registerParams.phone,
          'openid': openid,
          'cs': cs
        },
        success: function(res){
          console.log(res.data)
          var message = res.data.message
          var msg = res.data.msg
          if (msg == 'ok') {
            wx.showToast({
              title: '注册成功',
              icon: 'success',
              duration: 2000,
            })
            wx.setStorageSync('phone', that.data.registerParams.phone)
            // 转回主界面
            wx.request({
              url: config.url_root + '/isUserReged',
              data: { 'openid': openid },
              method: 'GET',
              success: function (res3) {
                console.log("res3: ", res3)
                if ('no' == res3.data.result) {
                  wx.redirectTo({
                    url: '/pages/register/register',
                  })
                }
                else {
                  var userType = res3.data.userType
                  if (userType == "manager") {
                    wx.redirectTo({
                      url: '/pages/manger/manager'
                    })
                  }
                  else {
                    wx.redirectTo({
                      url: '/pages/worker/worker'
                    })
                  }

                }
              }
            })
          } else if (msg == 'ng') {
            wx.showModal({
              title: '注册失败',
              icon: 'none',
              showCancel: false
            })
          }
        },
        fail: function() {
          that.failMessage()
        },
        complete: function() {
          // 隐藏loading
          that.setData({
            'loginBtnProperty.loading': false
          })
        }
      })
    }
  },

  failMessage: function() {
    wx.showToast({
        title: '连接服务器失败',
        icon: 'loading',
        duration: 2000,
     })
  }
})